from rest_framework import serializers

from goods.models import SKU
from orders.models import OrderInfo, OrderGoods

#获取所有订单
class OrderListSerializer(serializers.ModelSerializer):
    '''订单序列化器类'''
    create_time=serializers.DateTimeField(format='%Y-%m-d% %H:%M:%S')
    class Meta:
        model=OrderInfo
        fields=('order_id','create_time')



class SKUSimpleSerializer(serializers.ModelSerializer):
    '''订单商品序列化器类'''
    class  Meta:
        model=SKU
        fields=('id','name','default_image')

class OrderSKUSerializer(serializers.ModelSerializer):
    '''订单序列化器类'''
    #关联对象的嵌套序列化
    sku=SKUSimpleSerializer(label="SKU商品")
    class Meta:
        model=OrderGoods
        fields=('id','count','price','sku')
#获取订单详情
class OrderDetailSerializer(serializers.ModelSerializer):
    '''订单详情 序列化器类'''
    #关联对象的嵌套序列化器
    user=serializers.StringRelatedField(label='下单用户')
    skus=OrderSKUSerializer(label='订单商品',many=True)

    create_time=serializers.DateTimeField(format='%Y-%m-d% %H:%M:%S')

    class Meta:
        model=OrderInfo
        exclude=('update_time','address')

#订单装填
class OrderStatusSerializer(serializers.ModelSerializer):
    '''订单状态序列化器类'''
    class Meta:
        model=OrderInfo
        fields=('order_id','status')
        read_only_fields=('order_id',)